<?php
class CChipin extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;

	public function __construct()
	{
		$this->_name = R_PRE."chipin";
		$this->_primary = "ChipInID";
		parent::__construct();
	}
	
	public function add($row)
	{
		return $this->insert($row);
	}
	
	public function getAll($from,$counts,$fieldArray=null){
		$db=$this->getAdapter();
		$where = ' where 1=1';
		if(isset($fieldArray['MatchID']) && $fieldArray['MatchID'] != '')
		{
			$where .= ' and t1.MatchID='.$fieldArray['MatchID'];
		}
		$limit = " LIMIT $from, $counts";
		$sql = 'SELECT t1.*,t2.username,t3.TeamID FROM `zcshop_chipin` t1 left join `zcshop_members` t2 on t1.UID=t2.uid left join `zcshop_matchrate` t3 on t1.RateID=t3.MatchRateID' . $where . ' order by t1.ChipInID desc' . $limit;
		return $db->fetchAll($sql);
	}
	
	public function getAllByUser($from,$counts,$fieldArray=null){
		$db=$this->getAdapter();
		$where = ' where 1=1';
		if(isset($fieldArray['UID']) && $fieldArray['UID'] != '')
		{
			$where .= ' and t1.UID='.$fieldArray['UID'];
		}
		$limit = " LIMIT $from, $counts";
		$sql = 'SELECT t1.*,t2.username,t3.TeamID FROM `zcshop_chipin` t1 left join `zcshop_members` t2 on t1.UID=t2.uid left join `zcshop_matchrate` t3 on t1.RateID=t3.MatchRateID' . $where . ' order by t1.ChipInID desc' . $limit;
		return $db->fetchAll($sql);
	}

	public function getCounts($fieldArray=null){
		$db=$this->getAdapter();
		$select=$db->select();
		$select->from($this->_name,'COUNT(*)');
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$where = $db->quoteInto("$field = ?", $value);
				$select->where($where);
			}
		}
		return $db->fetchOne($select);
	}
	
	public function sendMailValue($matchId)
	{
		$db = $this->getAdapter();
		$sql = 'SELECT c.*, m.*, u.email, b.nick_name from `zcshop_chipin` c left join `zcshop_matchinfo` m on c.MatchID=m.MatchID left join `zcshop_members` u on c.UID=u.uid left join `zcshop_buyers` b on c.UID=b.uid where c.MatchID=' . $matchId . ' and c.Status=1';
		return $db->fetchAll($sql);
	}
}